package com.gitee.chili.leisure.api.mahjong;

import cn.hutool.json.JSONObject;
import com.gitee.chili.leisure.ResponseResult;
import com.gitee.chili.leisure.annotation.PathRestController;
import com.gitee.chili.leisure.domain.GamesMode;
import com.gitee.chili.leisure.exception.GameModeNotFoundException;
import com.gitee.chili.leisure.service.IGameTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import java.util.Optional;

/**
 * GameTypeController
 *
 * @author week solider
 * @since 2020.09.03
 */
@Slf4j
@Validated
@PathRestController("/gameType")
public class GameTypeController {


    @Autowired
    private IGameTypeService gameTypeService;


    @PostMapping("/getType")
    public ResponseResult<GamesMode> getType(@Valid JSONObject param) {
        Long gameType = Long.parseLong(param.get("gameType").toString());

        Optional<GamesMode> optional = gameTypeService.findById(gameType);

        if (optional.isPresent()) {
            return ResponseResult.success(optional.get());
        }

        log.error("未找到需要创建的房间类型： {}", gameType, new GameModeNotFoundException());
        return ResponseResult.failure("房间创建失败");
    }

}
